<?php

/* * ************************************************************************* 
 * item_price_model.class.php
 * Encoding : UTF-8
 * Desc. :商品价格数据库实现类
 * Created on : 2013-4-23 10:45:07 by Aiden , aiden.ke@doosnet.net
 * Updated on : 2013-4-23 10:45:07 by Aiden , aiden.ke@doosnet.net
 * ************************************************************************* */

defined('IN_SPIDER') or exit('No permission resources.');

pc_base::load_sys_class('model', '', 0);

class item_price_model extends model {

    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        $this->table_name = '';
        parent::__construct();
    }

    /**
     * 设置商品数据表
     * @param string $tablename 商城名称
     */
    public function table_name($mallname) {
        $this->table_name = $this->db_config[$this->db_setting]['tablepre'] . $mallname;
        return $this->table_name;
    }

    /**
     * 更新价格表信息（更新周期每天，保留30个节点）
     * @param table_name $string 表名
     * @param mall_id $string 来源ID
     * @param item_no $string 产品ID
     * @param price $strint 价格
     */
    public function update_price_table($table_name, $mall_id, $item_no, $price) {
        $data = array();
        $keys = array();
        $trendprice = 0;
        $time = getmicrotime(TRUE);
        $price = (string) $price;
        
        $sql = "select count(*) as cnt,`id`,`item_no`,`price_data` from `$table_name` where `item_no`='$item_no';";        
        $result = $this->query($sql);        
        if ($result['0']['cnt'] == 0) {
            if (is_numeric($price) && $price > 0) {
                $data = serialize(array($time => $price));
                $sql = "insert into `$table_name` set `item_no`=$item_no,`price_data`='$data';";
                $this->query($sql);
            } else {
                echo'价格为空,跳过更新价格' . "<hr/>";
            }
        } else {
            $data = array();
            $data = unserialize(stripslashes($result['0']['price_data']));
            $keys = array_keys($data);

            //节点总数
            $cnt = count($keys);

            //最后一次更新时间
            $lase_time = $keys[$cnt - 1];

            //最后一次更新价格
            $lase_price = $data[$lase_time];
            
            $is_update = $time - $lase_time > intval(pc_base::load_config('system', 'cycleTime')) ? TRUE : FALSE;
            if ($is_update) {//判断当天是否已更新过价格，若是，则跳过。
                //若大于结点数，则移除第一个;
                if ($cnt >= pc_base::load_config('system', 'price_nums')) {
                    $keys = array_keys($data);
                    unset($data[$keys[0]]);
                }

                //若价格未抓取到,取最后一个价格
                if (!is_numeric($price) || $price == 0 || !$price) {
                    echo '没有抓取到价格';
                    $price = $lase_price;
                } else {
                    $trendprice = $price - $lase_price;
                }

                $data[$time] = $price;
                $data = serialize($data);
                $sql = "update `$table_name` set `price_data`='" . $data . "'where  `item_no`='" . $result['0']['item_no'] . "';";
                $this->query($sql);
            } else {
                echo '今天已经更新过此ID:' . $item_no . "<hr/>";
            }
        }

        unset($result, $data, $sql, $keys, $time,$price,$sql);
        return $trendprice;
    }

}

?>